package com.example.dllo.exe.fenlei.gonglue;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.DanPinPinJieDownBean;
import com.example.dllo.exe.bean.GongLuePinJieBean;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.main.BaseAty;
import com.example.dllo.exe.fengzhuang.DBValue;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class GongLuePinJieAty extends BaseAty implements View.OnClickListener {
    private ListView listViewgongluepinjie;
    private TextView textViewbiaoti;
    private DanPinPinJieDownAdapter adapter;
    private RelativeLayout relativeLayout;
    private TextView textViewzhongxin;
    private ImageView headBgIv;
    private TextView headContentTv;
    private ImageView imageViewback;
    @Override
    protected int setLayout() {
        return R.layout.gongluepinjielayout;
    }

    @Override
    protected void initView() {
        textViewbiaoti = bindView(R.id.text_biaoti);
        listViewgongluepinjie = bindView(R.id.listitempinjie);
        relativeLayout= (RelativeLayout) findViewById(R.id.R_L);
       imageViewback= (ImageView) findViewById(R.id.gongluepinjieback);
        //注意下面的写法
        View headView = LayoutInflater.from(this).inflate(R.layout.gongluepingjietoplayout, listViewgongluepinjie, false);
        headBgIv = (ImageView) headView.findViewById(R.id.head_back_iv);
        headContentTv = (TextView) headView.findViewById(R.id.head_content_tv);
        textViewzhongxin = (TextView) headView.findViewById(R.id.text_viewzhongxin);
        //添加头布局
        listViewgongluepinjie.addHeaderView(headView);


    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("key");
        String name = intent.getStringExtra("name");
        int id = intent.getIntExtra("url", 0);
        textViewbiaoti.setText(name);//标题
        adapter = new DanPinPinJieDownAdapter(this);
        listViewgongluepinjie.setAdapter(adapter);
        Log.d("GongLuePinJieAty", url.toString());
        final NetTool netTool = new NetTool();
        netTool.getNetData(url, GongLuePinJieBean.class, new NetTool.NetListener<GongLuePinJieBean>() {
            @Override
            public void onSuccess(GongLuePinJieBean gongLuePinJieBean) {
                //简介
                headContentTv.setText(gongLuePinJieBean.getData().getDescription());
                netTool.getImage(gongLuePinJieBean.getData().getBanner_image_url(), headBgIv);
                textViewzhongxin.setText(gongLuePinJieBean.getData().getTitle());

            }

            @Override
            public void onError(String errorMsg) {

            }
        });


        String listUrl = DBValue.FENLEI_GONGLUEQIANZHUIUPGAIBIAN + id + DBValue.FENLEI_GONGLUEHOUZHUIDOWNGAIBIAN;
        Log.d("GongLuePinJieActivity1", listUrl.toString());
        netTool.getNetData(listUrl, DanPinPinJieDownBean.class, new NetTool.NetListener<DanPinPinJieDownBean>() {
            @Override
            public void onSuccess(DanPinPinJieDownBean danPinPinJieDownBean) {
                adapter.setBean(danPinPinJieDownBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        listViewgongluepinjie.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if (absListView.getChildAt(0) != null) {
                    getScrolly();
                    if (getScrolly() <= 0) {
                        relativeLayout.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));
                    } else if (getScrolly() > 0 && getScrolly() <= 200) {
                        float scale = (float) getScrolly() / 200;
                        float alpha = (225 * scale);
                        // 只是layout背景透明(仿知乎滑动效果)
                        relativeLayout.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
                    } else {
                        relativeLayout.setBackgroundColor(Color.argb((int) 225, 227, 29, 26));
                    }
                }
            }

            //获取listview当前滚动距离的方法
            private int getScrolly() {
                View view= listViewgongluepinjie.getChildAt(0);
                if (view == null) {
                    return 0;

                }
                int firstVisiblePosion=listViewgongluepinjie.getFirstVisiblePosition();
                int top = view.getTop();
                return -top + firstVisiblePosion * view.getHeight();
            }
        });
        imageViewback.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        finish();
    }
}
